<?php
/* System tray icon: used for minimising the window to (to hide it), provide a context menu
 *
 * Set tooltip:
 *   global $tray;
 *   $tray->set_tooltip("Manta")
 *
 * Add menu items:
 *   global $tray;
 *   $tray->create_menu_item("Name", array($this, "activation_callback_function"));
 */

class MantaTrayIcon extends GtkStatusIcon {
	protected $menu;
	//protected $menuitems = array();

	function __construct() {
		parent::__construct();
		$this->menu = new GtkMenu();
		$this->set_status();
		$this->connect_simple("activate", array($this, "activate_main_window"));
		$this->connect_simple("popup-menu", array($this, "on_menu"));

		$this->create_menu_item("_Hide/show main window", array($this, "toggle_main_window"));
		$this->create_separator();
		$this->create_menu_item("_Restart", "restart");
		$this->create_menu_item("_Quit", "quit");

		GtkStatusIcon::position_menu($this->menu, $this);
	}

	function activate_main_window() {
		global $window;
		$window->show_all();
		$window->present();
	}

	function toggle_main_window() {
		global $window;
		if ($window->is_visible()) $window->hide_all();
		else $window->show_all();
	}

	function on_menu() {
		$this->menu->popup();
	}

	function set_status($running=null) {
		if ($running === null) {
			$this->set_tooltip("Manta");
			$this->set_from_file(p("graphics/manta-logo.png"));
		} elseif ($running) {
			$this->set_tooltip("Manta - running");
			$this->set_from_file(p("graphics/status-running.png"));
		} else {
			$this->set_tooltip("Manta - not running");
			$this->set_from_file(p("graphics/status-stopped.png"));
		}
	}

	function create_separator() {
		$this->menu->append(new GtkSeparatorMenuItem());
	}

	function create_menu_item($name, $action) {
		$item = new GtkMenuItem($name);
		$item->connect_simple("activate", $action);
		$this->menu->append($item);
		//$this->menuitems[] = $item;
		$this->menu->show_all();
	}
}
